package com.tjetc.view.studentview;

import com.tjetc.proxy.CilentProxy;
import com.zhiyun.domain.Course;
import com.zhiyun.domain.Notice;
import com.zhiyun.domain.Signup;
import com.zhiyun.service.CourseService;
import com.zhiyun.service.NoticeService;
import com.zhiyun.service.SignupService;
import com.zhiyun.vo.AppData;
import com.zhiyun.vo.Result;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MyCourseView_In {
    SignupService signupService=CilentProxy.getProxyInstance(SignupService.class);
    CourseService courseService= CilentProxy.getProxyInstance(CourseService.class);
    NoticeService noticeService=CilentProxy.getProxyInstance(NoticeService.class);
    Scanner scanner=new Scanner(System.in);
    public void start(Course course){
        while (true){
            System.out.println("~~~~~~~欢迎进入"+course.getName()+"课程~~~~~~");
            System.out.println("1.查看课程内容");
            System.out.println("2.课程详细信息");
            System.out.println("3.签到");
            System.out.println("4.抢答");
            System.out.println("5.通知");
            System.out.println("6.评价");
            System.out.println("7.考试");
            System.out.println("0.返回上一级");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    new MyCourseView_In_Content().start(course);
                    break;
                case 2:
                    allInfo(course);
                    break;
                case 3:
                    qiandao(course);
                    break;
                case 4:
                    new MyCourseView_In_Answer().start(course);
                    break;
                case 5:
                    tongzhi(course);
                    break;
                case 6:
                    new MyCourseView_In_Comment().start(course);
                    break;
                case 7:
                    new MyCourseView_In_Exams().start(course);
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入有误,请重新输入");
            }
        }
    }
    private void allInfo(Course course) {
        while (true){
            Result byId = courseService.findById(course.getId());
            int code = byId.getCode();
            if (code==200){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                Course obj = (Course) byId.getObj();
                String s = sdf.format(obj.getBeginTime());
                String s1 = sdf.format(obj.getEndTime());
                System.out.println("课程名称"+obj.getName());
                System.out.println("课程描述"+obj.getDescribe());
                System.out.println("课程开课时间"+s);
                System.out.println("课程结束时间"+s1);
                System.out.println("课程任课老师"+obj.getTeacherId());
            }else {
                System.out.println("查询失败");
            }
            break;
        }
    }

    private void qiandao(Course course) {
        while (true){
            System.out.println("1.签到列表");
            System.out.println("2.选择签到");
            System.out.println("0.返回上一级");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    while (true){
                        Result result = signupService.mySignup(course.getId(), AppData.student.getId());
                        if (result.getCode()==200){
                            List<Signup> obj = (List<Signup>) result.getObj();
                            for (Signup signup : obj) {
                                System.out.println("签到id"+signup.getId()+"     是否签到:"+(signup.getIsSignup()==0?"没有签到":"已签到"));
                            }
                        }else {
                            System.out.println("无法获取签到详情");
                        }
                        break;
                    }
                    break;
                case 2:
                    while (true){
                        boolean a=false;
                        List<Signup> obj1 =new ArrayList<>();
                        Result result = signupService.mySignup(course.getId(), AppData.student.getId());
                        if (result.getCode()==200){
                            List<Signup> obj = (List<Signup>) result.getObj();

                            for (Signup signup : obj) {
                                if (signup.getIsSignup()==0){
                                    obj1.add(signup);
                                    System.out.println("签到id"+signup.getId()+"     是否签到:"+(signup.getIsSignup()==0?"没有签到":"已签到"));
                                }
                            }
                        }

                       if (obj1.size()!=0){
                           System.out.println("请选择想要签到的id");
                           int i1 = scanner.nextInt();
                           System.out.println("请输入签到码");
                           String next = scanner.next();
                           List<Signup> obj = (List<Signup>) result.getObj();
                           for (Signup signup : obj) {
                               if (i1==signup.getId()&&signup.getIsSignup()==0){
                                   a=true;
                               }
                           }
                           if (a){
                               Result result1 = signupService.signUp(i1, next);
                               if (result1.getCode()==200){
                                   System.out.println("签到成功");
                               }else {
                                   System.out.println("签到失败");
                               }
                           }
                       }else {
                           System.out.println("所有签到都已经签到了~~");
                       }
                        break;
                    }
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入有误,请重新输入");
            }
        }
    }

    private void tongzhi(Course course) {
        while (true){
            Result byCourseId = noticeService.findByCourseId(course.getId());
            if (byCourseId.getCode()==200){
                List<Notice> obj = (List<Notice>) byCourseId.getObj();
                for (Notice notice : obj) {
                    System.out.println("通知id"+notice.getId()+"      通知内容:"+notice.getMessage());
                }
            }else {
                System.out.println(byCourseId.getMsg());
            }
            break;
        }
    }


}
